#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=100000, maxm=500000;

long long max(long long a, long long b) {
	return a>b ? a : b;
}

class Graph {
	public:
		int n, m, ind[maxn+1], to[2*maxm+1], val[2*maxm+1], link[2*maxm+1];

		void addSide(int u, int v, int w) {
			m++;
			to[m] = v;
			val[m] = w;
			link[m] = ind[u];
			ind[u] = m;
		}

		long long run(int s, int t, long long v) {
			static bool book[maxn+1];
			if (s==t) {
				return v;
			} else {
				book[s] = true;
				long long ret=0;
				for (int i=ind[s]; i; i=link[i]) {
					if (!book[to[i]]) {
						ret = max(ret, run(to[i], t, v&val[i]));
					}
				}
				book[s] = false;
				return ret;
			}
		}
};

int main() {
	freopen("graph.in", "r", stdin);
	freopen("graph.out", "w", stdout);

	static Graph g;
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i=1; i<=m; i++) {
		int u, v, w;
		scanf("%d %d %d", &u, &v, &w);
		g.addSide(u, v, w);
		g.addSide(v, u, w);
	}

	printf("%lld", g.run(1, n, (1LLU<<63)-1));

	fclose(stdin);
	fclose(stdout);
	return 0;
}
